<?php

class Exam_AdminFaqController extends Core_Controller_Action_Admin {

    public function viewfaqAction() {
        // navigation
        $this->view->navigation = $navigation = Engine_Api::_()
                ->getApi('menus', 'core')
                ->getNavigation('exam_admin_main', array (), 'exam_admin_main_faq');
        $this->view->navigation_sub = $navigation = Engine_Api::_()->getApi('menus', 'core')
                ->getNavigation('exam_admin_main_faq', array(), 'exam_admin_main_faq_faq');

        $table = Engine_Api::_()->getDbtable('faqs', 'exam');
        $select = $table->select()
                ->setIntegrityCheck(false)
                ->from('engine4_exam_faqs as a')
                ->join('engine4_exam_faqcategories as c','c.faqcategory_id = a.faqcategory_id');
        
        $values = array();
        if(isset ($_POST)) {
            $values = $_POST;
        }

        $values = array_merge(array(
                'order' => 'faq_id',
                'order_direction' => 'DESC',
                ), $values);

        $this->view->assign($values);
        // Set up select info
        $select->order(( !empty($values['order']) ? $values['order'] : 'faq_id' ) . ' ' . ( !empty($values['order_direction']) ? $values['order_direction'] : 'DESC' ));

        $paginator = $this->view->paginator = Zend_Paginator::factory($select);
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber( $this->_getParam('page') );
    }
    public function addfaqAction() {
        $this->_helper->layout->setLayout('admin-simple');

        $this->view->form = $form = new Exam_Form_Admin_Faqcategory_AddFaq();

        if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {

            try {
                $values = $form->getValues();
                $table = Engine_Api::_()->getDbTable('faqs','exam')->createRow();
                $table->setFromArray($values);
                $table->save();
            } catch (Exception $e) {
                throw $e;
            }

            $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => true,
                    'parentRefresh' => true,
                    'format' => 'smoothbox',
                    'messages' => array($this->view->translate('FAQ added successfully.'))
            ));
        }
    }
    public function editfaqAction() {
        $this->_helper->layout->setLayout('admin-simple');
        $objFaq = new Exam_Model_DbTable_Faqs(array());
        $faq_id = $this->_getParam('faq_id', null);

        $this->view->form = $form = new Exam_Form_Admin_Faqcategory_EditFaq();

        $row = $objFaq->getFaqById($faq_id);

        if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
            $values = $form->getValues();
            try {
                $row->question = $values['question'];
                $row->answer = $values['answer'];
                $row->faqcategory_id = $values['faqcategory_id'];
                $row->save();
            } catch (Exception $e) {
                throw $e;
            }

            $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => true,
                    'parentRefresh' => true,
                    'format' => 'smoothbox',
                    'messages' => array($this->view->translate('FAQ edited successfully.'))
            ));
        }
        $form->populate($row->toArray());
    }
    public function deletefaqAction() {
        $objFaq = new Exam_Model_DbTable_Faqs(array());

        $this->view->form = $form = new Exam_Form_Admin_Faqcategory_DeleteFaq();

        $faq_id = $this->_getParam('faq_id', null);
        $row = $objFaq->getFaqById($faq_id);
        if ($this->getRequest()->isPost()) {
            try {
                $row->delete();
            } catch (Exception $e) {
                throw $e;
            }

            $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => true,
                    'parentRefresh' => true,
                    'format' => 'smoothbox',
                    'messages' => array($this->view->translate('FAQ deleted.'))
            ));
        }
    }
    public function viewcategoryAction() {
        // navigation
        $this->view->navigation = $navigation = Engine_Api::_()
                ->getApi('menus', 'core')
                ->getNavigation('exam_admin_main', array (), 'exam_admin_main_faq');
        $this->view->navigation_sub = $navigation = Engine_Api::_()->getApi('menus', 'core')
                ->getNavigation('exam_admin_main_faq', array(), 'exam_admin_main_faq_category');

        $table = Engine_Api::_()->getDbtable('faqcategories', 'exam');
        $select = $table->select()->order('faqcategory_name');
        $paginator = $this->view->paginator = Zend_Paginator::factory($select);
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber( $this->_getParam('page') );
    }

    public function addfaqcategoryAction() {
        $this->_helper->layout->setLayout('admin-simple');

        $objFaqcategories = new Exam_Model_DbTable_Faqcategories(array());

        $this->view->form = $form = new Exam_Form_Admin_Faqcategory_AddFaqcategories();

        $faqcategory_name = $this->_getParam('faqcategory_name');

        if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
            if(count($objFaqcategories->getFaqcategoriesByName(trim($faqcategory_name))) > 0) {
                $this->_forward('success', 'utility', 'core', array(
                        'smoothboxClose' => 2000,
                        //'parentRefresh' => true,
                        'format' => 'smoothbox',
                        'messages' => array($this->view->translate('message exist faq category'))
                ));
                return;
            }
            try {
                $objFaqcategories->addFaqcategories($faqcategory_name);
            } catch (Exception $e) {
                throw $e;
            }


            $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => true,
                    'parentRefresh' => true,
                    'format' => 'smoothbox',
                    'messages' => array($this->view->translate('Categories added successfully.'))
            ));
        }
    }

    public function editfaqcategoryAction() {
        $this->_helper->layout->setLayout('admin-simple');
        $objFaqcategories = new Exam_Model_DbTable_Faqcategories(array());
        $faqcategory_id = $this->_getParam('faqcategory_id', null);

        $this->view->form = $form = new Exam_Form_Admin_Faqcategory_EditFaqcategories();

        $row = $objFaqcategories->getFaqcategoriesById($faqcategory_id)->toArray();
        $faqcategory_name = $this->_getParam('faqcategory_name');

        if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
            if ($faqcategory_name == $row['faqcategory_name']) {
                $this->_forward('success', 'utility', 'core', array(
                        'smoothboxClose' => true,
                        //'parentRefresh' => true,
                        'format' => 'smoothbox',
                        'messages' => array($this->view->translate('Faq categories edited successfully.'))
                ));
                return;
            }

            if($faqcategory_name != $row['faqcategory_name'] && count($objFaqcategories->getFaqcategoriesByName(trim($faqcategory_name))) > 0) {
                $this->_forward('success', 'utility', 'core', array(
                        'smoothboxClose' => 2000,
                        //'parentRefresh' => true,
                        'format' => 'smoothbox',
                        'messages' => array($this->view->translate('message exist faq category'))
                ));
                return;
            }

            try {
                $objFaqcategories->editFaqcategories($faqcategory_id, $faqcategory_name);
            } catch (Exception $e) {
                throw $e;
            }

            $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => true,
                    'parentRefresh' => true,
                    'format' => 'smoothbox',
                    'messages' => array($this->view->translate('Categories edited successfully.'))
            ));
        }
        $form->populate(array('faqcategory_name' => $row['faqcategory_name']));
    }

    public function deletefaqcategoryAction() {
        $objFaqcategories = new Exam_Model_DbTable_Faqcategories(array());

        $this->view->form = $form = new Exam_Form_Admin_Faqcategory_DeleteFaqcategories();

        $faqcategory_id = $this->_getParam('faqcategory_id', null);

        if ($this->getRequest()->isPost()) {
            try {
                //$objFaqcategories->deleteFaqcategories($faqcategory_id);
                $cat=Engine_Api::_()->getItem('exam_faqcategory',$faqcategory_id);
                $cat->delete();
            } catch (Exception $e) {
                throw $e;
            }

            $this->_forward('success', 'utility', 'core', array(
                    'smoothboxClose' => true,
                    'parentRefresh' => true,
                    'format' => 'smoothbox',
                    'messages' => array($this->view->translate('Categories deleted.'))
            ));
        }
    }
}